package com.syezon.wifikey.ad;

import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.ViewModel;

import com.blankj.utilcode.util.AppUtils;
import com.hodanet.appadvplatclient.AdvClient;
import com.hodanet.appadvplatclient.ad.AdInfo;
import com.hodanet.appadvplatclient.api.BaseResult;
import com.syezon.wifikey.ad.config.ConfigObserver;
import com.syezon.wifikey.channel.ChannelConfig;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;


/**
 * Created by Administrator on 2019/1/16.
 */

public class SplashAdViewModel extends ViewModel implements ConfigObserver {

    public static final String KEY_PREFIX = "SPLASH_VIEW_MODEL_";

    protected MutableLiveData<List<AdInfo>> adLiveData = new MutableLiveData<>();

    private AdPositionEnum adPositionEnum;

    private static List<AdInfo> EMPTY = new ArrayList<>();


    public SplashAdViewModel() {
    }

    public void setAdPositionEnum(AdPositionEnum adPositionEnum) {
        this.adPositionEnum = adPositionEnum;
        ConfigService.getInstance().registerObserver(this);
    }

    public MutableLiveData<List<AdInfo>> getAdLiveData() {
        return adLiveData;
    }

    @Override
    public void onChange(boolean showAd) {
        if (showAd && adPositionEnum != null) {
            getData(adPositionEnum);
        }
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        ConfigService.getInstance().unRegisterObserver(this);
    }

    protected void getData(final AdPositionEnum adPositionEnum) {
        Observable.create(new ObservableOnSubscribe<BaseResult<List<AdInfo>>>() {
            @Override
            public void subscribe(ObservableEmitter<BaseResult<List<AdInfo>>> e) throws Exception {
                BaseResult<List<AdInfo>> advs = AdvClient.getInstance().getAdvs(adPositionEnum.getPosId(), ChannelConfig.getChannel(), AppUtils.getAppVersionCode());
                e.onNext(advs);
            }
        }).subscribeOn(Schedulers.io())
                .subscribe(new Observer<BaseResult<List<AdInfo>>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BaseResult<List<AdInfo>> listBaseResult) {
                        if (listBaseResult != null && listBaseResult.getData() != null) {
                            adLiveData.postValue(listBaseResult.getData());
                        } else {
                            adLiveData.postValue(EMPTY);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
